 


	local variab "`1'"	
	local p "`2'"
	local c "`3'" 
	local filenom "`4'"
		
	*example:
	*local variab "female bwaba somelycee treatment"	
	*local p "hasportable hasportablefriend abonnee01 atleast1liv30 cantrust"
	*local c "age2013 educannee livres30 misspelltravmere misspelltravpere" 
	
	* These two blocks 'pass' the the variables from the original command to this do file with quotes
	* around each variable
	local i=1
	foreach var in `p' {
	if "`i'"=="1" {
		local ttp "`" "`var'" "'"
		}
	else if "`i'"~="1" {
		local ttp  "`ttp'"  "`" "`var'" "'"
		}
		local i=`i'+1
		}
			
	local i=1
	foreach var in `c' {
	if "`i'"=="1" {
		local ttc "`" "`var'" "'"
		}
	else if "`i'"~="1" {
		local ttc  "`ttc'"  "`" "`var'" "'"
		}
		local i=`i'+1
		}

	*loc varxx `variab' - `var'
	*insheet using "estfemale.csv", clear
* Column-wise table of summary statistics 
* variab are the categorical BY variables
* ttc are the continuous variables
* ttp are the 0-1 variables

	foreach var in `variab' {
	quietly {
	eststo clear
	local replace replace
	foreach tt in `c' `p' `variab' {
	bysort `var': eststo: quietly estpost summarize `tt' , listwise
	quietly esttab using "est`var'.csv", cells("mean sd count") nodepvar `replace' plain   
	eststo clear
	local replace append
	}
	}
	
	quietly {
	preserve
	insheet using "est`var'.csv", clear
	drop if v1=="" | v1=="N"
	* create a new row for the sample size
	local new = _N + 1
    set obs `new'
	replace v1="Sample size" if _n==_N
	destring, replace
	
	* generate variables to distinguish proportions from continuous variables
	gen prop=.
	foreach z in `p' `variab' {
		 replace prop=1 if v1=="`z'"
		 }
	gen cont=.
	foreach z in `c' `variab' {
		 replace cont=1 if v1=="`z'"
		 }
	loc proportions "if prop==1"
	loc continuous "if cont==1"
	
	* v5-v6-v7 is the "baseline" group to compare with other
	* for each by-group there are three observations - mean-sd-count

	foreach i in 2  {
	local j=`i'+1
	local k=`i'+2
	* Find the t-stat for means and the pval for that tstat (distributed student t) comparing with the reference group
	gen va`i'=(v`i'-v5)/(((v`j'^2/v`k')+(v6^2/v7))^(1/2)) `continuous'
	gen df`i'= round( (((v`j'^2/v`k')+(v6^2/v7))^2)/ (((v`j'^2/v`k')^2/(v`k'-1))+((v6^2/v7)^2/(v7-1))) )
	gen vb`i'= 2*ttail(df`i',abs(va`i')) `continuous'
	* Find the z-stat for proportions and the pval for that zstat (distributed normally)
	gen poolsamva`i'=(v`i'*v`k' + v5*v7)/(v`k'+v7)
	replace va`i'=(v`i'-v5)  /  (   (poolsamva`i'*(1-poolsamva`i')) * ((1/v`k')+(1/v7))    )^(1/2) `proportions'
	replace vb`i'=2*normal(-abs(va`i')) `proportions'
	order va`i', after(v`i')
	order vb`i', after(va`i')
	* New entry of the max number of observations for each group
	* And put in bottom of column of means
	egen vc`k'=max(v`k')
	replace v`i' = vc`k' if v`i'==.
	gen vd`i'=""
	replace vd`i'="*" if vb`i'<=.10
	replace vd`i'="**" if vb`i'<=.05
	replace vd`i'="***" if vb`i'<=.01
	*order vd`i', after(vb`i')
	drop poolsamva`i' df`i' vc`k'
	}
	egen vc7=max(v7)
	replace v5=vc7 if v5==.
	drop vc7
	keep v1 v2 vd2 v5 
	rename v2 v2`var'
	rename v5 v5`var'
	rename vd2 vd2`var'
	label var v1 " "
	foreach i in 2  {
	label var vd`i' " "
	}
	label var v2`var' "0 `var'"
	label var v5`var' "1 `var'"
	save e_`var', replace
	erase "est`var'.csv"
	restore	
	}
	}
	* now merge the datasets which are in same variable order
	
	quietly {
	local i=1
	foreach var in `variab' {
	if "`i'"=="1" {
		use e_`var', clear
		}
	else if "`i'"~="1" {
		merge 1:1 _n using e_`var'
		drop _merge
		}
	local i=`i'+1
	}
	foreach z in `variab'  {
	replace v2`z'=round(v2`z',.01)
	replace v5`z'=round(v5`z',.01)
	}
	}	
	
	local new = _N + 5
    set obs `new'
	replace v1="Notes: * p<0.10 ** p<0.05 *** p<0.01." if  _n==_N-4
	export excel using "`filenom'", sh(rawdat1) sheetreplace first(varl) 

	/*
	replace v1="Note: Asterisks indicate whether signficant statistical differencebetween treatment and control group, or treatment" if  _n==_N-4
	replace v1="and non-selected group, respectively * p<0.10 ** p<0.05 *** p<0.01. Means and standard deviations and differences " if  _n==_N-3
	replace v1="between means are calculated for each variable with maximum data available for that variable; some variables," if  _n==_N-2
	replace v1="such as books read in previous 30 days, had missing observations.  Complete data across all variables is available for 664 of the 696 applicants;" if  _n==_N-1
	replace v1="means and differences using only the complete sample are qualitatively very similar." if  _n==_N
	*/



